<?php

namespace app\common\model\xiservice;

use think\Model;


class User extends Model
{

    

    

    // 表名
    protected $name = 'xiservice_user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'avatar_url',
        'mobile_text',
        'work_start_time_text',
        'work_end_time_text',
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getMobileTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['mobile']) ? $data['mobile'] : '');
        if(!$value){
            return '';
        }
        return substr_replace( $value,'****',3,4);
    }

    public function getAvatarUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['avatar']) ? $data['avatar'] : '');
        return  cdnurl($value,true);
    }

    public function getWorkStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['work_start_time']) ? $data['work_start_time'] : '09:00:00');
        $array = explode(':', $value);
        $first = intval($array[0]) +1;
        if($array[1]=='00'){
            $index = 0;
        }else{
            $index = 1;
        }
        return [$first,$index];
    }

    public function getWorkEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['work_end_time']) ? $data['work_end_time'] : '18:00:00');
        $array = explode(':', $value);
        $first = intval($array[0]) +1;
        if($array[1]=='00'){
            $index = 0;
        }else{
            $index = 1;
        }
        return [$first,$index];
    }

    /**
     * 变更会员已提现金额
     * @param int    $money   draw_money
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function drawmoney($money, $user_id)
    {
        $user = self::get($user_id);
        if ($user && $money != 0) {
            $before = $user->draw_money;
            $after = bcadd($before, $money, 2);
            //更新会员信息
            $user->save(['draw_money' => $after]);
        }
    }    

    /**
     * 变更会员可提现金额
     * @param int    $money   can_money
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo)
    {
         $user = self::where('user_id',$user_id)->find();
        if ($user && $money != 0) {
            $before = $user->can_money;
            $after = bcadd($before, $money, 2);
            //更新会员信息
            $user->save(['can_money' => $after]);
            //写入日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 增加会员冻结金额
     * @param int    $money   freeze_money
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function freeze($money, $user_id, $order_id,$type)
    {
        $user = self::where('user_id',$user_id)->find();
        if ($user && $money != 0) {
            $before = $user->freeze_money;
            $after = bcadd($before, $money, 2);
            //更新会员信息
            $user->save(['freeze_money' => $after]);
            //写入日志
            Benfit::create(['user_id' => $user_id, 'money' => $money, 'order_id' => $order_id, 'type' => $type]);
        }
    }

    /**
     * 减少会员提现冻结金额
     * @param int    $money   freeze_money
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function unfreeze($user_id,$money,$ids)
    {
         $user = self::where('user_id',$user_id)->find();
        if ($user && $money != 0) {
            $before = $user->freeze_money;
            $after = bcsub($before, $money, 2);
            //更新会员信息
            $user->save(['freeze_money' => $after]);
            //删除日志
            Benfit::where('order_id',$ids)->where('user_id',$user_id)->where('type',2)->delete();
        }

    }

}
